package com.fjzn.detect.config.web.mvc;

import com.fjzn.detect.config.web.interceptor.ApiSignAccessHandlerInterceptor;
import com.fjzn.detect.config.web.interceptor.RepeatSubmitInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 拦截器配置
 *
 * @author yzh
 * @since 2021年04月25日16:58:21
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Value("${sign.open}")
    private boolean opensign;

    /**
     * 文件下载排除
     */
    private static final List<String> EXCLUDE_FILE_PATH = new ArrayList<>();

    static {
        EXCLUDE_FILE_PATH.add("/**/common/download/**");
    }

    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;

    @Autowired
    private ApiSignAccessHandlerInterceptor apiSignAccessHandlerInterceptor;

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {"classpath:/static/" };

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if(opensign){
            registry.addInterceptor(apiSignAccessHandlerInterceptor).excludePathPatterns("/static/**").excludePathPatterns(EXCLUDE_FILE_PATH);
        }
        registry.addInterceptor(repeatSubmitInterceptor).excludePathPatterns("/static/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
        registry.setOrder(1);
    }

}
